/**
 *
 */
package com.zhuoyue.elmt.web;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.zhuoyue.elmt.dao.FailureReportDao;
import com.zhuoyue.elmt.data.ResultData;
import com.zhuoyue.elmt.entity.FailureReplacementPart;
import com.zhuoyue.elmt.entity.FailureReport;
import com.zhuoyue.elmt.service.FailureReportService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.eit.eitsite.common.config.Global;
import com.eit.eitsite.common.persistence.Page;
import com.eit.eitsite.common.web.BaseController;
import com.eit.eitsite.common.utils.StringUtils;
import com.zhuoyue.elmt.entity.FailureSurveyContent;
import com.zhuoyue.elmt.service.FailureSurveyContentService;

import java.util.ArrayList;
import java.util.List;

/**
 * 电梯故障调查内容Controller
 *
 * @author chenhao
 * @version 2016-09-07
 */
@Controller
@RequestMapping(value = "${adminPath}/elmt/failureSurveyContent")
public class FailureSurveyContentController extends BaseController {

    @Autowired
    private FailureSurveyContentService failureSurveyContentService;

    @Autowired
    private FailureReportService failureReportService;

    @ModelAttribute
    public FailureSurveyContent get(@RequestParam(required = false) String id) {
        FailureSurveyContent entity = null;
        if (StringUtils.isNotBlank(id)) {
            entity = failureSurveyContentService.get(id);
        }
        if (entity == null) {
            entity = new FailureSurveyContent();
        }
        return entity;
    }

    @RequiresPermissions("elmt:failureSurveyContent:view")
    @RequestMapping(value = {"list", ""})
    public String list(FailureSurveyContent failureSurveyContent, HttpServletRequest request, HttpServletResponse response, Model model) {
        Page<FailureSurveyContent> page = failureSurveyContentService.findPage(new Page<FailureSurveyContent>(request, response), failureSurveyContent);
        model.addAttribute("page", page);
        return "zhuoyue/elmt/failureSurveyContentList";
    }

    @RequiresPermissions("elmt:failureSurveyContent:view")
    @RequestMapping(value = "form")
    public String form(FailureSurveyContent failureSurveyContent, Model model) {
        model.addAttribute("failureSurveyContent", failureSurveyContent);
        return "zhuoyue/elmt/failureSurveyContentForm";
    }

    @RequiresPermissions("elmt:failureSurveyContent:edit")
    @RequestMapping(value = "save")
    public String save(FailureSurveyContent failureSurveyContent, Model model, RedirectAttributes redirectAttributes) {
        if (!beanValidator(model, failureSurveyContent)) {
            return form(failureSurveyContent, model);
        }
        failureSurveyContentService.save(failureSurveyContent);
        addMessage(redirectAttributes, "保存调查内容成功");
        return "redirect:" + Global.getAdminPath() + "/elmt/failureSurveyContent/?repage";
    }

    @RequiresPermissions("elmt:failureSurveyContent:edit")
    @RequestMapping(value = "delete")
    public String delete(FailureSurveyContent failureSurveyContent, RedirectAttributes redirectAttributes) {
        failureSurveyContentService.delete(failureSurveyContent);
        addMessage(redirectAttributes, "删除调查内容成功");
        return "redirect:" + Global.getAdminPath() + "/elmt/failureSurveyContent/?repage";
    }

    /**
     * 查询单条电梯故障调查内容记录
     *
     * @param str1
     * @param response
     * @return
     */
    @RequestMapping(method = RequestMethod.GET, value = "/getFailureSurveyContentInfo/{str1}")
    public String getFailureSurveyContentInfo(@PathVariable String str1, HttpServletResponse response) {
        ResultData<List<FailureSurveyContent>> resultData = new ResultData();
        List<FailureSurveyContent> list = new ArrayList();
        try {
            System.out.println("查询单条电梯故障调查内容记录 ===========================================>>>>>>>>>>>>>>");
            FailureSurveyContent record = failureSurveyContentService.get(str1);
            if (record != null) {
                resultData.setCode("SUCC");
                resultData.setMessage("SUCCESS");
            } else {
                resultData.setCode("FAIL");
                resultData.setMessage("没有查询到符合条件的电梯故障调查内容记录！");
            }
            list.add(record);
            resultData.setList(list);
        } catch (Exception e) {
            resultData.setCode("FAIL");
            resultData.setMessage("电梯故障调查内容记录查询出现异常！");
            e.printStackTrace();
        }
        return super.renderString(response, resultData);
    }

    /**
     * 查询多条电梯故障调查内容记录
     *
     * @param failureSurveyContent
     * @param request
     * @param response
     * @return
     */
    @RequestMapping(method = RequestMethod.GET, value = "/findFailureSurveyContentInfo")
    public String findFailureSurveyContentInfo(FailureSurveyContent failureSurveyContent, HttpServletRequest request, HttpServletResponse response) {
        ResultData<List<FailureSurveyContent>> resultData = new ResultData();
        try {
            System.out.println("查询多条电梯故障调查内容记录 ===========================================>>>>>>>>>>>>>>");
            Page<FailureSurveyContent> page = failureSurveyContentService.findPage(new Page<FailureSurveyContent>(request, response), failureSurveyContent);
            if (null != page) {
                resultData.setList(page.getList());
            }
            resultData.setCode("SUCC");
            resultData.setMessage("");
        } catch (Exception e) {
            resultData.setCode("FAIL");
            resultData.setMessage("电梯故障调查内容记录查询出现异常！");
            e.printStackTrace();
        }
        return super.renderString(response, resultData);
    }

    @RequestMapping(method = RequestMethod.POST, value = "/saveFailureSurveyContentInfo")
    public String saveFailureSurveyContentInfo(FailureReport failureReport, FailureSurveyContent failureSurveyContent, Model model, HttpServletResponse response) {
        ResultData<List<FailureSurveyContent>> resultData = new ResultData();
        List<FailureSurveyContent> list = new ArrayList();
        try {
            System.out.println("增加电梯故障调查内容记录 ===========================================>>>>>>>>>>>>>>");
            if (!beanValidator(model, failureSurveyContent)) {
                return form(failureSurveyContent, model);
            }
            failureSurveyContentService.save(failureSurveyContent);
            System.out.println("failureSurveyContent.getContentId() ================>>>>>>>>>>>>>> " + failureSurveyContent.getContentId());

            //增加返回model
            list.add(failureSurveyContent);
            resultData.setList(list);

            resultData.setFormId(failureSurveyContent.getContentId());
            resultData.setCode("SUCC");
            resultData.setMessage("增加电梯故障调查内容记录成功");
        } catch (Exception e) {
            resultData.setCode("FAIL");
            resultData.setMessage("增加电梯故障调查内容记录出现异常！");
            e.printStackTrace();
        }
        return super.renderString(response, resultData);
    }

    @RequestMapping(method = RequestMethod.POST, value = "/updateFailureSurveyContentInfo")
    public String updateFailureSurveyContentInfo(FailureSurveyContent failureSurveyContent, Model model, HttpServletResponse response) {
        ResultData<List<FailureSurveyContent>> resultData = new ResultData();
        List<FailureSurveyContent> list = new ArrayList();

        try {
            System.out.println("更新电梯故障调查内容记录 ===========================================>>>>>>>>>>>>>>");
            if (!beanValidator(model, failureSurveyContent)) {
                return form(failureSurveyContent, model);
            }
            if (StringUtils.isNotEmpty(failureSurveyContent.getContentId())) {
                failureSurveyContentService.update(failureSurveyContent);

                //增加返回model
                list.add(failureSurveyContent);
                resultData.setList(list);

                resultData.setCode("SUCC");
                resultData.setMessage("更新电梯故障调查内容记录成功");
            }
        } catch (Exception e) {
            resultData.setCode("FAIL");
            resultData.setMessage("更新电梯故障调查内容记录出现异常！");
            e.printStackTrace();
        }
        return super.renderString(response, resultData);
    }

    @RequestMapping(method = RequestMethod.POST, value = "/deleteFailureSurveyContentInfo")
    public String deleteFailureSurveyContentInfo(FailureSurveyContent failureSurveyContent, Model model, HttpServletResponse response) {
        ResultData<List<FailureSurveyContent>> resultData = new ResultData();
        List<FailureSurveyContent> list = new ArrayList();
        try {
            System.out.println("删除电梯故障调查内容记录 ===========================================>>>>>>>>>>>>>>");
            if (!beanValidator(model, failureSurveyContent)) {
                return form(failureSurveyContent, model);
            }
            failureSurveyContentService.delete(failureSurveyContent);

            resultData.setFormId(failureSurveyContent.getContentId());
            resultData.setCode("SUCC");
            resultData.setMessage("删除电梯故障调查内容记录成功");
        } catch (Exception e) {
            resultData.setCode("FAIL");
            resultData.setMessage("删除电梯故障调查内容记录出现异常！");
            e.printStackTrace();
        }
        return super.renderString(response, resultData);
    }

    @RequestMapping(method = RequestMethod.POST, value = "/saveContent")
    public String saveContent(FailureReport failureReport, String[] contentId, String[] surveyItem, String[] surveySituation, String deviceCode, String failureNo, HttpServletResponse response) {
        ResultData<List<FailureSurveyContent>> resultData = new ResultData();
        List<FailureSurveyContent> list = new ArrayList();
        try {
            System.out.println("电梯故障调查内容记录 ===========================================>>>>>>>>>>>>>>");
            FailureReport qryFailureReport = new FailureReport();
            qryFailureReport.setFailureNo(failureNo);
            FailureReport newFailureReport = failureReportService.getByFailureNo(qryFailureReport);
            // 更新 基本情况 到达时间，设备处理状态、完成时间、维保人员姓名、备注内容放在故障处理标签页。
            newFailureReport.setArrivalTime(failureReport.getArrivalTime());
            newFailureReport.setProcessStatus(failureReport.getProcessStatus());
            newFailureReport.setCompletionTime(failureReport.getCompletionTime());
            newFailureReport.setMaintenanceStaff1(failureReport.getMaintenanceStaff1());
            newFailureReport.setMaintenanceStaff2(failureReport.getMaintenanceStaff2());
            newFailureReport.setFailureDeal(failureReport.getFailureDeal()); // 故障处理过程
            newFailureReport.setRemark(failureReport.getRemark());
            newFailureReport.setStatus(failureReport.getStatus());

            if (contentId != null && contentId.length > 0) {
                // 如果只有一条数据
                if (contentId.length == 1) {
                    for (int i = 0; i < contentId.length; i++) {
                        FailureSurveyContent failureSurveyContent = new FailureSurveyContent();
                        if (!"0".equals(contentId[i])) {
                            failureSurveyContent.setContentId(contentId[i]);
                        }
                        failureSurveyContent.setDeviceCode(deviceCode);
                        failureSurveyContent.setFailureNo(failureNo);
                        if (StringUtils.isNotEmpty(surveyItem[i])) {
                            failureSurveyContent.setSurveyItem(surveyItem[i]);
                        }
                        if (StringUtils.isNotEmpty(surveySituation[i])) {
                            failureSurveyContent.setSurveySituation(surveySituation[i]);
                        }
                        //增加返回model
                        list.add(failureSurveyContent);
                    }
                } else {
                    for (int i = 0; i < contentId.length; i++) {
                        FailureSurveyContent failureSurveyContent = new FailureSurveyContent();
                        if (!"0".equals(contentId[i])) {
                            failureSurveyContent.setContentId(contentId[i]);
                        }
                        failureSurveyContent.setDeviceCode(deviceCode);
                        failureSurveyContent.setFailureNo(failureNo);
                        failureSurveyContent.setSurveyItem(surveyItem[i]);
                        failureSurveyContent.setSurveySituation(surveySituation[i]);

                        //增加返回model
                        list.add(failureSurveyContent);
                    }
                }
            }

            failureSurveyContentService.saveOrUpdate(newFailureReport, list);

            resultData.setList(list);
            resultData.setFailureReport(newFailureReport);

            resultData.setCode("SUCC");
            resultData.setMessage("电梯故障调查内容记录成功");
        } catch (Exception e) {
            resultData.setCode("FAIL");
            resultData.setMessage("电梯故障调查内容记录出现异常！");
            e.printStackTrace();
        }
        return super.renderString(response, resultData);
    }
}